package com.sc.nft.service.impl;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.sc.nft.dto.SendMessageMassReq;
import com.sc.nft.enums.ApplicationRequirementUpOrDownEnum;
import com.sc.nft.enums.WorkCityServerAssessDetailEnum;
import com.sc.nft.feign.SmsFeignClient;
import com.sc.nft.service.ApplicationRequirementSmsService;
import com.sc.nft.service.SmsCodeRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;


@Slf4j
@RequiredArgsConstructor
@Service
public class ApplicationRequirementSmsServiceImpl implements ApplicationRequirementSmsService {
    private final SmsFeignClient smsFeignClient;
    private final RedisTemplate redisTemplate;
    private final SmsCodeRecordService smsCodeRecordService;

    @Override
    public void sendSms(String phone, WorkCityServerAssessDetailEnum workCityServerAssessDetailEnum) {
        String iPhone = phone.substring(phone.length() - 4);
//        List<String> messages = new ArrayList<>();
//        // phone获取最后四位
//        messages.add(StrUtil.format("尾号{}", iPhone));
//        messages.add(DateUtil.now());
//        messages.add(workCityServerAssessDetailEnum.getDescribe());
//        SendMessageMassReq sendMessageMassReq = new SendMessageMassReq();
//        sendMessageMassReq.setTemplateId(4075L);
//        sendMessageMassReq.setContentVariable(messages);
//        sendMessageMassReq.setPhone(phone);
//        smsFeignClient.sendMessageMass(sendMessageMassReq);
        String[] data = {StrUtil.format("尾号{}", iPhone),DateUtil.now(),workCityServerAssessDetailEnum.getDescribe()};
        smsCodeRecordService.sendTemplate(phone,"2231925",data);
    }

    /**
     * 城市服务中心预升降级短信通知
     *
     * @param phone
     * @param applicationRequirementUpOrDownEnum
     */
    @Override
    public void sendPreSms(String phone, ApplicationRequirementUpOrDownEnum applicationRequirementUpOrDownEnum) {
        //region 测试开关
//        phone = "15045598808"; //刘洋的手机号码
//        if (phone.equals("15045598808")) {
//            String format = StrUtil.format("sendPreSms:{}", phone);
//            if (redisTemplate.hasKey(format)) {
//                return;
//            }
//            redisTemplate.opsForValue().set(format, format, 60 * 60 * 24 * 7);
//        } else {
//            return;
//        }
        //endregion 测试开关
//        List<String> messages = new ArrayList<>();
//        messages.add(phone);
//        messages.add(applicationRequirementUpOrDownEnum.getDescribe());
//        SendMessageMassReq sendMessageMassReq = new SendMessageMassReq();
//        sendMessageMassReq.setTemplateId(4123L);
//        sendMessageMassReq.setContentVariable(messages);
//        sendMessageMassReq.setPhone(phone);
//        smsFeignClient.sendMessageMass(sendMessageMassReq);

        String[] data = {phone, applicationRequirementUpOrDownEnum.getDescribe()};
        smsCodeRecordService.sendTemplate(phone,"2231924",data);
    }

}
